package com.ld.admin.controller.system.outdatabase;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.codegen.CodegenDbsetModel;
import com.ld.admin.model.system.outdatabase.OutType;
import com.ld.admin.service.codegen.CodegenService;
import com.ld.admin.service.outdatabase.OutDatabaseService;
import com.ld.shieldsb.codegen.service.CodeGenService;
import com.ld.shieldsb.common.core.util.PathUtil;
import com.ld.shieldsb.db.model.DBSetInfo;
import com.ld.shieldsb.db.model.DBTableColumnModel;
import com.ld.shieldsb.db.model.DBTableModel;
import com.ld.shieldsb.db.service.DBTabelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName OutDatabaseController
 * @Description 输出表结构 控制器
 * @Author 付信豪
 * @Date 2019/8/5 14:32
 * @ModifyDate 2019/8/5 14:32
 * @Version 1.0
 */
@Api(tags = "输出表结构")
@ConditionalOnProperty(prefix = "modules.sc.system.outdatabase", name = "enable", havingValue = "true", matchIfMissing = true)
@Controller
@RequestMapping("${shieldsb.adminPath}/system/outdatabase")
public class OutDatabaseController extends AdminBasicController<CodegenDbsetModel> {
    @Autowired
    public CodegenService codegenService;
    @Autowired
    public OutDatabaseService outDatabaseService;

    /**
     * @Description 跳转到输出表结构页面
     * @Author 付信豪
     * @Date 2019/8/6 17:31
     * @ModifyDate 2019/8/6 17:31
     * @Params []
     * @Return java.lang.String
     */
    @ApiOperation(value = "输出表结构页面", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/system/outdatabase/toSearch"; // 跳转搜索页面
    }

    /**
     * @Description 跳转搜索页面
     * @Author 付信豪
     * @Date 2019/8/6 17:30
     * @ModifyDate 2019/8/6 17:30
     * @Params []
     * @Return java.lang.String
     */
    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() throws ServletException, IOException {
        // 搜索数据库设置
        List<CodegenDbsetModel> dbSetList = basicService.getList(CodegenDbsetModel.class, basicService.getNormalQueryModel());
        request.setAttribute("dbsets", dbSetList);
        return "a/system/codegen/outdatabase/outDatabaseSearch";
    }

    /**
     * @Description 跳转到列表页面
     * @Author 付信豪
     * @Date 2019/8/6 17:30
     * @ModifyDate 2019/8/6 17:30
     * @Params []
     * @Return java.lang.String
     */
    @ApiOperation(value = "搜索表主要信息", notes = "")
    @RequestMapping(path = "/tables", method = RequestMethod.POST)
    public String tables() throws ServletException, IOException {
        setAttributes();
        Long dbConnId = getLong("conn");
        DBSetInfo dbSetInfo = codegenService.getDbSetInfo(dbConnId);
        DBTabelService service = DBTabelService.getInstance(dbSetInfo);
        String tableName = getString("tableName");
        List<DBTableModel> dbList = service.getTables(tableName);
        request.setAttribute("list", dbList); // list
        return "a/system/codegen/outdatabase/outDatabaseTables";
    }

    /**
     * @Description 跳转到详细页面
     * @Author 付信豪
     * @Date 2019/8/6 17:29
     * @ModifyDate 2019/8/6 17:29
     * @Params []
     * @Return java.lang.String
     */
    @ApiOperation(value = "显示表结构", notes = "")
    @RequestMapping(path = "/databaseShow", method = RequestMethod.GET)
    public String tablecols() throws ServletException, IOException {
        setAttributes();
        request.setAttribute("projectPath", PathUtil.getProjectPath());
        String tableName = getString("tableName");
        String className = CodeGenService.parseClassName(tableName); // 表名转类名
        request.setAttribute("className", className);
        Long dbConnId = getLong("conn");
        DBSetInfo dbSetInfo = codegenService.getDbSetInfo(dbConnId);
        DBTabelService service = DBTabelService.getInstance(dbSetInfo);
        List<DBTableColumnModel> tablCols = service.getTableColumn(tableName);
        request.setAttribute("list", tablCols); // list
        return "a/system/codegen/outdatabase/outDatabaseShow";
    }

    /**
     * @Description 导出word表结构
     * @Author 林昊
     * @Date 2019/8/12 11:36
     * @ModifyDate 2019/8/12 11:36
     * @Params []
     * @Return org.springframework.http.ResponseEntity<org.springframework.core.io.FileSystemResource>
     */
    @ApiOperation(value = "下载word表结构", notes = "")
    @RequestMapping(path = "/exportword", method = RequestMethod.POST)
    public ResponseEntity<FileSystemResource> exportWord() throws IOException {
        setAttributes();
        Long dbConnId = getLong("conn");
        String[] tableNames = getArray("tableName");// 获取表名
        return outDatabaseService.outFile(tableNames, dbConnId, OutType.WORD);
    }

    /**
     * @Description 导出excel表结构
     * @Author 付信豪
     * @Date 2019/8/6 17:29
     * @ModifyDate 2019/8/6 17:29
     * @Params []
     * @Return org.springframework.http.ResponseEntity<org.springframework.core.io.FileSystemResource>
     */
    @ApiOperation(value = "导出excel表结构", notes = "")
    @RequestMapping(path = "/exportexcel", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public ResponseEntity<FileSystemResource> exportExcel() throws IOException {
        setAttributes();
        Long dbConnId = getLong("conn");
        String[] tableNames = getArray("tableName");// 获取表名
        return outDatabaseService.outFile(tableNames, dbConnId, OutType.EXCEL);
    }

    /**
     * 
     * 导出PDF表结构
     * 
     * @Title exportPdf
     * @author 吕凯
     * @date 2019年10月15日 下午2:00:16
     * @return
     * @throws IOException
     *             ResponseEntity<FileSystemResource>
     */
    @ApiOperation(value = "导出PDF表结构", notes = "")
    @RequestMapping(path = "/exportpdf", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public ResponseEntity<FileSystemResource> exportPdf() throws IOException {
        setAttributes();
        Long dbConnId = getLong("conn");
        String[] tableNames = getArray("tableName");// 获取表名
        return outDatabaseService.outFile(tableNames, dbConnId, OutType.PDF);
    }
}